import { API } from '../api'
import { DateLike, convertTime } from '../utils'

/** 获取用户 */
export const getUser = async (id: number) =>
	(await API.getUserInfo([['id', 0, id + '', 0]]))[1]

/** 获取用户名称 */
export const getUserNames = async (ids: number[]) => {
	const [, ...users] = await API.getUserInfo([['', -1, `id in (${ids.join(',')})`, 0]])
	return users.map(u => u.name)
}

/**
 * 获取协作者
 * @param pid 帖子ID
 */
export const getCollaborator = async (pid: number) =>
	await API.getCR([['', -1, `pid=${pid} and status=2`, 0]])

/**
 * 获取用户的时间戳
 * @param uid 用户ID
 * @param start 开始时间
 * @param end 结束时间
 * @returns 时间戳列表
 */
export async function getTimestamps(uid: number, start: DateLike, end: DateLike) {
	const [, ...cr] = await API.getCR([['', -1, `creator=${uid} and created_at between ${convertTime(start)} and ${convertTime(end)}`, 0]])
	return cr.map(c => c.created_at)
}